package com.zisuyi.uilibrary.module.dialog


import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.viewbinding.ViewBinding
import com.weilele.mvvm.base.helper.createViewBinding
import com.weilele.mvvm.utils.activity.dip
import com.weilele.mvvm.utils.activity.onClick
import com.weilele.mvvm.utils.activity.textFrom
import com.weilele.mvvm.utils.safeGet
import com.zisuyi.uilibrary.R
import com.zisuyi.uilibrary.base.BaseUiDialog
import com.zisuyi.uilibrary.databinding.DialogBaseTitleBinding


abstract class BaseTitleDialog<Binding : ViewBinding> : BaseUiDialog<Binding>() {

    private var baseBinding: DialogBaseTitleBinding? = null
    private var _binding: Binding? = null

    override val mBinding: Binding
        get() = _binding!!

    override fun getLayoutResOrView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): Any? {
        baseBinding = DialogBaseTitleBinding.inflate(inflater)
        _binding = createViewBinding(inflater)
        _binding?.root?.let {
            baseBinding?.root?.addView(it)
        }
        return baseBinding?.root
    }

    override fun onRootViewLayoutParams(lp: FrameLayout.LayoutParams) {
        super.onRootViewLayoutParams(lp)
        lp.marginStart = dip(50)
        lp.marginEnd = lp.marginStart
    }

    override fun initUi(savedInstanceState: Bundle?) {
        super.initUi(savedInstanceState)
        baseBinding?.ivDialogClose.onClick {
            dismiss()
        }
    }


    fun setTitle(title: CharSequence?) {
        baseBinding?.tvDialogTitle?.textFrom(title)
    }

    fun setTitle(title: Int) {
        baseBinding?.tvDialogTitle?.textFrom(title)
    }

    fun setOnCLickCloseListener(onSingleClick: Function1<View, Unit>?) {
        baseBinding?.ivDialogClose?.onClick(onSingleClick)
    }
}